package com.java.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.common.model.Result;
import com.java.manage.dao.LicenseplateMapper;
import com.java.manage.model.Color;
import com.java.manage.model.Licenseplate;
import com.java.manage.service.LicenseplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LicenseplateImpl implements LicenseplateService {

    @Autowired
    private LicenseplateMapper licenseplateMapper;


    @Transactional
    @Override
    public Result save(Licenseplate licenseplate) {
        licenseplate.setStatus("1");
        licenseplateMapper.save(licenseplate);

        return Result.success();
    }

    @Transactional
    @Override
    public Result modify(Licenseplate licenseplate) {
        licenseplateMapper.modify(licenseplate);

        return Result.success();
    }

    @Transactional
    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            licenseplateMapper.remove(id);
        }

        return Result.success();
    }

    @Override
    public Result list(Licenseplate licenseplate) {
        PageHelper.startPage(licenseplate.getPageNum(), licenseplate.getPageSize());
        List<Licenseplate> list = licenseplateMapper.list(licenseplate);
        PageInfo<Licenseplate> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());

        return Result.success(map);
    }

    @Override
    public Result get(int id) {
        Licenseplate book = licenseplateMapper.get(id);

        return Result.success(book);
    }

    @Override
    public Result plateList() {
        List<Licenseplate> licenseplates = licenseplateMapper.plateList();
        return Result.success(licenseplates);
    }
}
